package com.daohang361.imqq.ui.headtree;

import java.awt.Color;
import java.awt.Component;
import java.awt.Cursor;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.Box;
import javax.swing.JLabel;
import javax.swing.JPanel;

import com.csu.client.module.vo.MsgUser;
import com.daohang361.imqq.ui.EIMMainFrame;
import com.daohang361.imqq.ui.EIMTalkFrame;
import com.daohang361.ui.interfaces.IMQQLogin;

public class EIMTreeNode extends JPanel implements MouseListener{

	private MsgUser user;
	private UserPopoInfo popoInfo;
	
	public EIMTreeNode(MsgUser user) {
		this.addMouseListener(this);
		this.user = user;
		priantNode(user);
	}
	public String formate(String s){
		int length1=0;
		int length2=0;
		if(s!=null&&s.length()!=0)
		{
		 length1 = s.getBytes().length;
		 length2 = s.length();
		}else
			s="";
		int length=length2+(length1-length2)/2;
		StringBuilder temps=new StringBuilder();
		temps.append(s);
		for(int i=0;i<30-length;i++)
		{
			temps.append(" ");
		}
		return temps.toString();
	}
	public void priantNode(MsgUser user){
		
		Box box=Box.createHorizontalBox();
		box.setAlignmentX(Component.LEFT_ALIGNMENT);
		String headImage=(String)user.getAttribute("headImage");
		NodeIcon nodeIcon=new NodeIcon(headImage,user.isOnline());
		nodeIcon.setAlignmentX(Component.LEFT_ALIGNMENT);
		box.add(nodeIcon);
		
		Box verticalBox=Box.createVerticalBox();
		JLabel personWordLale=new JLabel();
		personWordLale.setText(user.getUserName());
		JLabel kongjianLale=new JLabel();
		kongjianLale.setText(formate(user.getPersonWord()));
		verticalBox.add(personWordLale);
		verticalBox.add(kongjianLale);
		verticalBox.setAlignmentX(Component.LEFT_ALIGNMENT);
		box.add(verticalBox);
		
		this.add(box);
		this.setBackground(Color.white);
		this.setVisible(false);
	}

	public void mouseClicked(MouseEvent e) {
		System.out.println(user.getUserName()+"Clicked");
	}

	public void mouseEntered(MouseEvent e) {
		setBackground(new java.awt.Color(102, 204, 255));
		setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
		popoInfo =new UserPopoInfo(this.user);
		EIMMainFrame v=EIMMainFrame.getInstance();
		popoInfo.setLocation(v.getX()-popoInfo.getWidth(), this.getY()+200);
		popoInfo.setVisible(true);
	}

	public void mouseExited(MouseEvent e) {
		setBackground(Color.white);
		setCursor(Cursor.getDefaultCursor());
		popoInfo.setVisible(false);
	}

	public void mousePressed(MouseEvent e) {
		if(e.getClickCount()==2){
			EIMTalkFrame talkFrame=new EIMTalkFrame(user);
			System.out.println("EIMTREENODE");
			talkFrame.setOwerUser(IMQQLogin.ower);
			System.out.println(user.getUserName());
			talkFrame.setVisible(true);
		}
		
	}

	public void mouseReleased(MouseEvent e) {
		
	}

	public MsgUser getUser() {
		return user;
	}

	public void setUser(MsgUser user) {
		this.user = user;
	}

}
